package main

import (
	"fmt"
	"time"
)

func main() {
	var ch chan string = make(chan string, 5)
	var ch1 chan string = make(chan string, 5)

	go putData(ch)
	go putData(ch1)
	/*for {
		value, ok := <-ch
		if ok {
			fmt.Println("value is", value)
		} else {
			fmt.Println("over")
			break
		}

	}*/

	for {
		select {
		case v, ok := <-ch:
			if ok {
				fmt.Println("ch1----value is", v)
			} else {
				fmt.Println("ch----over")
				return
			}
		case v, ok := <-ch1:
			if ok {
				fmt.Println("ch1----value is", v)
			} else {
				fmt.Println("ch1----over")
				return
			}
		case <-time.After(2 * time.Second):
			fmt.Println("time out")
			return

		}

	}

}

func putData(ch chan string) {
	defer func() {
		fmt.Println("happensBeforeChan is close ")
		close(ch)
	}()
	goTime := "2006-01-02 15:04:05"
	for i := 0; i < 5; i++ {
		time.Sleep(1 * time.Second)
		ch <- time.Now().Format(goTime)
		fmt.Println("putData-time", time.Now(), " index=", i)

	}
}
